import { createSlice, createAsyncThunk, PayloadAction } from '@reduxjs/toolkit';
import axios from 'axios';

export interface ListState {
  id: string
  desc: string
  title: string
  price: string
}

export const fetchList = createAsyncThunk(
  'fetchList',
  async (q: string, thunkAPI) => {
    const resp = await axios.get('/api/list', { params: { q: q } });
    thunkAPI.dispatch(setList(resp.data.data))
  }
)



const listSlice = createSlice({
  name: 'listSlice',
  initialState: {
    list: [] as ListState[]
  },
  reducers: {
    setList(state, action) {
      state.list = action.payload
    }
  }
})

export default listSlice.reducer
export const { setList } = listSlice.actions